1672B - I love AAAB - CodeForces Solution


constructive algorithms implementation *800

Please click on ads to support us..

Python Code:

for _ in [0]*int(input()):
 s=input();a=b=0;ans='YES'
 for i in s:
  a+=(i=='A');b+=(i=='B')
  if a<b:ans='NO';break
 print(ans if s[-1]=='B' else 'NO')

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
const int N=2e5+10;
#define INF 0x3f3f3f3f;
typedef long long int ll;
#define close(); std::ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);
//----------------------------------------------------------------------------//

void solve()
{
	string s1;
	cin>>s1;
	bool have_b=false;
	bool ok=true;

	int num_a=0;
	int num_b=0;
	for(int i=0;i<s1.size();i++)
	{
		if(s1[i]=='A') num_a++;
		if(s1[i]=='B')
		{
			have_b=true,num_b++;
			if(num_a>=num_b) continue;
			else
			{
				ok=false;
				break;
			}
			
		}
	}
	if(s1[s1.size()-1]=='A') ok=false;	

	if(ok&&have_b) cout<<"YES"<<'\n';
	else cout<<"NO"<<'\n';

	
}

int main()
{
	close();
	int T; cin>>T;
	while (T--) solve();
	return 0;
}

//AAAABAAABA


Comments

Submit
0 Comments
More Questions

1143B - Nirvana
1285A - Mezo Playing Zoma
919B - Perfect Number
894A - QAQ
1551A - Polycarp and Coins
313A - Ilya and Bank Account
1469A - Regular Bracket Sequence
919C - Seat Arrangements
1634A - Reverse and Concatenate
1619C - Wrong Addition
1437A - Marketing Scheme
1473B - String LCM
1374A - Required Remainder
1265E - Beautiful Mirrors
1296A - Array with Odd Sum
1385A - Three Pairwise Maximums
911A - Nearest Minimums
102B - Sum of Digits
707A - Brain's Photos
1331B - Limericks
305B - Continued Fractions
1165B - Polycarp Training
1646C - Factorials and Powers of Two
596A - Wilbur and Swimming Pool
1462B - Last Year's Substring
1608B - Build the Permutation
1505A - Is it rated - 2
169A - Chores
765A - Neverending competitions
1303A - Erasing Zeroes